<?php
/* MTEXPORT.INC.PHP by martin $ 2009/12/01 03:31:44
*/

checkADMIN();

function html2mtexport($yyyymm){
 global $back, $_FILTER, $_SMILIES, $TAGS_LIST, $DIVISION;
 $log = dirname('index.php').'/'.LOG."$yyyymm".EXT;
 if(!is_file($log)){
  return $DIVISION['header'] .= "<h4>ファイル: $log が見当たりません。</h4>";
 }

 $new = DB.$yyyymm.'_MT'.EXT;
 $LINES = file($log);
 $mt_out = '';
 include_once (OD.'tags.ini.php');
 
 foreach ($LINES as $line){
  list($id,$cat,$title,$com,$author,$cmt,$allowPing,$excerpt,$extended) = explode('|', $line);
  $TAGS = array();
  $hits = preg_grep("/$id/", $TAGS_LIST);
  if(!empty($hits)){
   foreach ($hits as $tag){
    list($tag,) = explode('|', $tag);
    $tag = str_replace(array_values($_FILTER), array_keys($_FILTER), $tag);
    $TAGS[] = $tag;
   }
  }
  if(!empty($TAGS)) $TAGS = implode(',', $TAGS);
  if(strstr($cat, ',')){
   list($main, $sub) = explode(',', $cat);
  } else $main = $cat;
  
  $extended_body = '';
  if(!empty($extended)){
   $_extended = explode(',', $extended);
   foreach ($_extended as $i=>$_ext){
    $_ext = str_replace(array_values($_FILTER), array_keys($_FILTER), $_ext);
    $extended_body .= (($i==0?NL:'').'EXTENDED BODY:'.NL.trim(str_replace('`', NL, $_ext)).NL.'-----'.NL);
   }
  }
  $com = str_replace('`', NL, str_replace(array_values($_FILTER), array_keys($_FILTER), $com));
  $comment = '';
  $comments = get_cmt_array($id);
  if(count($comments) > 1){
   foreach ($comments as $j => $c){
    if($j > 0){
    list($c_id, $c_name, , $c_com, $c_ip, $c_url, $c_mail) = explode('|', $c);
     $comment .= ($j==1?NL:'').'COMMENT:
AUTHOR: '.$c_name.'
DATE: '.date('m/d/Y H:i:s', $c_id).NL.
(trim($c_url)!=''?'URL: '.$c_url.NL:'').($c_ip!=0?'IP: '.$c_ip.NL:'').(trim($c_mail)!=''?'EMAIL: '.my_decrypt($c_mail).NL:'')
.trim(str_replace('`', NL, $c_com)).NL.'-----'.NL;
    }
   }
  } # comments
  
  $ping = $data = '';
  if(is_file(TB_DIR.$id.EXT)){
   $data = unserialize(get_file_content(TB_DIR.$id.EXT));
  }
  if(is_array($data)){
   $size = count($data);
   for($i = 0; $i < $size; $i++){
    $blog_name = !empty($data[$i]['blog_name']) ? rawurldecode($data[$i]['blog_name']) : rawurldecode($data[$i]['url']);
    $blog_name = sanitize_data($blog_name);
    $excerpt = sanitize_data(str_replace('`','NL',strip_tags(rawurldecode($data[$i]['excerpt']))));
    $excerpt = autolink($excerpt);
    $t_ip = gethostbyname(rawurldecode($data[$i]['url']));
    $title = sanitize_data(rawurldecode($data[$i]['title']));
    $ping_date = date('m/d/Y H:i:s', $data[$i]['entry']);
    $ping = ($i==0?NL:'').'PING:
TITLE: '.$title.'
URL: '.rawurldecode($data[$i]['url']).(preg_match('{^\d+\d$}', $t_ip)?NL.'IP: '.$t_ip:'').NL.'
BLOG NAME: '.$blog_name.'
DATE: '.$ping_date.
$excerpt.'
-----'.NL;
   }
  }  # ping
  
  $mt_out .= 'AUTHOR: '.$author.'
TITLE: '.$title.'
STATUS: '.(strpos($id, 'd') ? 'Draft' : 'Publish').'
ALLOW COMMENTS: '.$cmt.'
ALLOW PINGS: '.$allowPing.'
CONVERT BREAKS: '.ENABLE_BR.'
PRIMARY CATEGORY: '.$main.
(isset($sub)?NL.'CATEGORY: '.$sub : '').'
DATE: '.date('m/d/Y H:i:s', $id).NL.
(empty($TAGS) ? '' : 'TAGS: '.$TAGS.NL).'
-----
BODY:
'.$com.NL.'-----'.NL.trim($extended_body.$comment.$ping).NL.'--------'.NL;
 } # foreach
 $_LINES = array();
 $LINES = explode(NL, $mt_out);
 foreach ($LINES as $line){
  if(trim($line)=='') continue;
  $_LINES[] = $line;
 }
 $mt_out = implode(NL, $_LINES);
 if(preg_match_all('{<img src="([^"]*?)" alt="([^"]*?)" ?/>}i', $mt_out, $mt)){ // icluding image-tags
  $imgs = array();
  $length = count($mt[0]);
  for($i=0; $i < $length; $i++){
   $thumb = str_replace('PIX/', 'PIX/s2/', $mt[1][$i]);
   $imgs[$i] = is_file($thumb) ? $thumb : $mt[1][$i];
  }
  $mt_out = str_replace($mt[1], $imgs, $mt_out);
 }
 rewrite($new, $mt_out);
 update_MT($yyyymm, time());
 header("Content-Type: application/octet-stream; name=\"".basename($new)."\"");
 header("Content-Disposition: attachment; filename=\"".basename($new)."\"");
 header("Content-Length: ".filesize(PATH.$new));
 readfile(PATH.$new);
 unlink($new);
 exit;
}

?>